//
//  ViewController.m
//  scinanFanPadTestiOSPorject
//
//  Created by liuzhu on 2018/9/3.
//  Copyright © 2018年 liuzhu. All rights reserved.
//

#import "ViewController.h"
#import "FLSocketManager.h"
#import "SVProgressHUD.h"
#import "XHToast.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+JDragon.h"
@interface ViewController ()<FLSocketManagerDelegate>
@property(nonatomic,strong)FLSocketManager* socketManager;
@property (weak, nonatomic) IBOutlet UISlider *mySlider;

@end

@implementation ViewController
{
    NSString* _temCommand;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.socketManager = [[FLSocketManager alloc] init];
    self.socketManager.delegate = self;
    self.mySlider.continuous = NO;
    [self.mySlider setValue:1.0f];
    UIImage *thumbImage = [UIImage imageNamed:@"椭圆39拷贝2"];
   // UIImage *thumbImage = [UIImage imageNamed:@"椭圆47"];
    [self.mySlider setThumbImage:thumbImage forState:UIControlStateNormal];
}

#pragma mark 顶灯控制
- (IBAction)topLightA:(UIButton *)sender
{
    if (sender.tag == 0)
    {
         [self sendCommand:@"{\"Device\":\"TLA\",\"Value\":1}"];
        sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"TLA\",\"Value\":0}"];
        sender.tag = 0;
    }
   
}
- (IBAction)topLightB:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self sendCommand:@"{\"Device\":\"TLB\",\"Value\":1}"];
        sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"TLB\",\"Value\":0}"];
         sender.tag = 0;
    }
}
- (IBAction)topLightC:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self sendCommand:@"{\"Device\":\"TLC\",\"Value\":1}"];
         sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"TLC\",\"Value\":0}"];
         sender.tag = 0;
    }
}

- (IBAction)wallLightA:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self sendCommand:@"{\"Device\":\"WLA\",\"Value\":1}"];
        sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"WLA\",\"Value\":0}"];
        sender.tag = 0;
    }
}
- (IBAction)wallLightB:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self sendCommand:@"{\"Device\":\"WLB\",\"Value\":1}"];
        sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"WLB\",\"Value\":0}"];
        sender.tag = 0;
    }
}
- (IBAction)wallLightC:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self sendCommand:@"{\"Device\":\"WLC\",\"Value\":1}"];
        sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"WLC\",\"Value\":0}"];
        sender.tag = 0;
    }
}


- (IBAction)mediaAction:(UIButton *)sender
{
    if (sender.tag == 0)
    {
        [self sendCommand:@"{\"Device\":\"ME\",\"Value\":1}"];
        sender.tag = 1;
    }
    else
    {
        [self sendCommand:@"{\"Device\":\"ME\",\"Value\":0}"];
        sender.tag = 0;
    }
}

- (IBAction)ledPlay:(UIButton *)sender
{
     [self sendCommand:@"{\"Device\":\"LED\",\"Value\":1}"];
}

- (IBAction)ledStop:(UIButton *)sender
{
    [self sendCommand:@"{\"Device\":\"LED\",\"Value\":0}"];
}

- (IBAction)mvPlay:(UIButton *)sender
{
    switch (sender.tag) {
        case 1:
            [self sendCommand:@"{\"Device\":\"MV1\",\"Value\":1}"];
            break;
        case 2:
            [self sendCommand:@"{\"Device\":\"MV2\",\"Value\":1}"];
            break;
        case 3:
            [self sendCommand:@"{\"Device\":\"MV3\",\"Value\":1}"];
            break;
        case 4:
            [self sendCommand:@"{\"Device\":\"MV4\",\"Value\":1}"];
            break;
        default:
            break;
    }
}

- (IBAction)mvStop:(UIButton *)sender
{
    switch (sender.tag) {
        case 1:
            [self sendCommand:@"{\"Device\":\"MV1\",\"Value\":0}"];
            break;
        case 2:
            [self sendCommand:@"{\"Device\":\"MV2\",\"Value\":0}"];
            break;
        case 3:
            [self sendCommand:@"{\"Device\":\"MV3\",\"Value\":0}"];
            break;
        case 4:
            [self sendCommand:@"{\"Device\":\"MV4\",\"Value\":0}"];
            break;
        default:
            break;
    }
}

- (IBAction)musicAction:(UIButton *)sender
{
      switch (sender.tag) {
          case 1:
              [self sendCommand:@"{\"Device\":\"MU\",\"Value\":1}"];
              break;
          default:
              [self sendCommand:@"{\"Device\":\"MU\",\"Value\":0}"];
              break;
      }
}

- (IBAction)muSoundAction:(UISlider *)sender
{
    float value = sender.value;
    NSInteger valueInt = (NSInteger)value;
    NSString* command = [NSString stringWithFormat:@"{\"Device\":\"MCS\",\"Value\":%ld}",(long)valueInt];
    
    [self sendCommand:command];
}

-(void)sendCommand:(NSString*)command
{
    NSLog(@"%@",command);
    _temCommand = command;
    if (self.socketManager.isConnetced)
    {
         [self.socketManager sendDataWith:command andTag:1];
    }
    else
    {
        [MBProgressHUD showActivityMessageInWindow:@"正在建立连接..."];
        self.view.userInteractionEnabled = NO;
        [self.socketManager connectToDeviceWithIp:@"192.168.8.100" port:@"8500" timeout:30 reserve:@{}];
    }
}

//- (IBAction)link:(UIButton *)sender
//{
//    [SVProgressHUD show];
//    self.view.userInteractionEnabled = NO;
//    [self.socketManager connectToDeviceWithIp:@"192.168.8.100" port:@"8500" timeout:30 reserve:@{}];
//    self.statusLabel.text = @"连接中...";
//}
//
//- (IBAction)close:(UIButton *)sender
//{
//    if (self.socketManager.isConnetced)
//    {
//         self.statusLabel.text = @"断开中...";
//    }
//    else
//    {
//         self.statusLabel.text = @"未连接";
//    }
//}

#pragma mark - FLSocketManager
-(void)socketConnected
{
    self.view.userInteractionEnabled = YES;
    [MBProgressHUD showSuccessMessage:@"连接成功"];
    if (self.socketManager.isConnetced)
    {
        [self sendCommand:_temCommand];
    }
    [MBProgressHUD hideHUD];

}

-(void)socketDisconnected
{
     [MBProgressHUD showInfoMessage:@"连接已断开"];

}

-(void)socketFailConnected
{
    [MBProgressHUD hideHUD];
    [MBProgressHUD showTipMessageInWindow:@"连接失败，请检查网络或设备IP地址是否正确(192.168.8.100:8500)" timer:5];
     self.view.userInteractionEnabled = YES;
}

-(void)socketReceiveData:(NSString *)message andTag:(NSInteger)tag
{
    NSLog(@"收到数据:%@",message);
}

@end
